package main

import (
	"fmt"
	"maps"
	"strings"
	"unsafe"
)

type User struct {
	ID   int
	Name string
}

func main() {
	m := make(map[int]*User)
	m[1] = &User{1, "1111"}
	m[2] = &User{2, "2222"}
	fmt.Printf("addr of m: %v\n", unsafe.Pointer(&m))
	fmt.Printf("addr of m[1]: %v\n", unsafe.Pointer(m[1]))
	fmt.Printf("addr of m[2]: %v\n", unsafe.Pointer(m[2]))
	{ //shallow copy
		mc := maps.Clone(m)
		fmt.Printf("addr of mc: %v\n", unsafe.Pointer(&mc))
		fmt.Printf("addr of mc[1]: %v\n", unsafe.Pointer(mc[1]))
		fmt.Printf("addr of mc[2]: %v\n", unsafe.Pointer(mc[2]))
	}
	{ // copy from src to dest, override existing kvs
		dest := make(map[int]*User)
		dest[1] = nil
		maps.Copy(dest, m)
		fmt.Printf("addr of mc: %v\n", unsafe.Pointer(&dest))
		fmt.Printf("addr of mc[1]: %v\n", unsafe.Pointer(dest[1]))
		fmt.Printf("addr of mc[2]: %v\n", unsafe.Pointer(dest[2]))
		fmt.Println(dest)
		fmt.Println(maps.Equal(m, dest)) //true
	}
	{
		//进用于比较value，key仍然使用==比较
		m1 := map[int]string{1: "one", 10: "Ten", 1000: "THOUSAND"}
		m2 := map[int][]byte{1: []byte("One"), 10: []byte("Ten"), 1000: []byte("Thousand")}
		eq := maps.EqualFunc(m1, m2, func(v1 string, v2 []byte) bool {
			return strings.ToLower(v1) == strings.ToLower(string(v2))
		})
		fmt.Println(eq)
	}
	{
		//删除符合条件的
		m := map[string]int{"one": 1, "two": 2, "three": 3, "four": 4}
		maps.DeleteFunc(m, func(k string, v int) bool {
			return v%2 != 0 // delete odd values
		})
		fmt.Println(m)
	}
}
